import React, { Component, PropTypes } from 'react';
import { Router, Route, IndexRoute, browserHistory, Link,hashHistory } from 'react-router';
import {Tool} from '../Tool';
import * as config from '../Config';
import {Api} from "../Api";
const Util = {};
const {target,pathImage} = config;


/* 验证是否为空 */
Util.IsNull = function (obj) {
    var flag = false;
    if (obj == null || obj == undefined || typeof (obj) == 'undefined' || obj == '') {
        flag = true;
    } else if (typeof (obj) == 'string') {
        obj = obj.trim();
        if (obj == '') {//为空
            flag = true;
        } else {//不为空
            obj = obj.toUpperCase();
            if (obj == 'NULL' || obj == 'UNDEFINED' || obj == '{}') {
                flag = true;
            }
        }
    }
    else {
        flag = false;
    }
    return flag;
}


Util.Trim = function(str,is_global){
    var result;
    result = str.replace(/(^\s+)|(\s+$)/g,"");
    if(is_global.toLowerCase()=="g"){
        result = result.replace(/\s/g,"");
    }
    return result;
}

/*
 * 验证是否为手机号
 * */
Util.Phone= function (obj) {
    if(Util.IsNull(obj)){
        return false;
    }
    if(!(/^1(3|4|5|7|8)\d{9}$/.test(obj))){
        return false;
    }
    return true;
}

Util.getApi = (key)=> {
    let obj=null;
    for(let i=0,len=Api.length;i<len;i++){
        if(Api[i].key==key){
            obj=Api[i];
            return obj.url ;
        }
    }
};

Util.getField=()=>{
    return [
        {title:"电子电气",id:7,isActive:false},
        {title:"纺织服装",id:8,isActive:false},
        {title:"化学化工",id:9,isActive:false},
        {title:"计算机",id:10,isActive:false},
        {title:"能源环保",id:11,isActive:false},
        {title:"机械设备",id:12,isActive:false},
        {title:"家用电器",id:13,isActive:false},
        {title:"建筑建材",id:14,isActive:false},
        {title:"农林牧渔",id:15,isActive:false},
        {title:"轻工制造",id:16,isActive:false},
        {title:"食品饮料",id:17,isActive:false},
        {title:"新材料",id:18,isActive:false},
        {title:"生物医药",id:19,isActive:false},
        {title:"其他",id:20,isActive:false}
    ];
}

Util.getFieldById=(id)=>{
    var title = "",nId = 0;
    Util.getField().map(function(i){
        if(i.id==id){
            title = i.title;
        }
    })
    return title;
}

Util.getFieldByName=(name)=>{
    var id = 0;
    Util.getField().map(function(i){
        if(i.title==name){
            id = i.id;
        }
    })
    return id;
}

Util.getHonors=()=>{
    return [
        {title:"中国科学院院士",id:1,isActive:false},
        {title:"中国工程院院士",id:2,isActive:false},
        {title:"长江学者",id:3,isActive:false},
        {title:"千人计划",id:4,isActive:false},
        {title:"青年千人计划",id:5,isActive:false},
        {title:"青年长江学者",id:6,isActive:false},
        {title:"国家杰出青年科学基金",id:7,isActive:false},
        {title:"优秀青年基金",id:8,isActive:false},
        {title:"百千万人才工程",id:9,isActive:false},
        {title:"新世纪优秀人才	",id:10,isActive:false},
    ];
}

Util.getHonorsById=(id)=>{
    var title = "";
    Util.getHonors().map(function(i){
        if(i.id==id){
            title = i.title;
        }
    })
    return title;
}

Util.getHonorsByName=(name)=>{
    var id = 0;
    Util.getHonors().map(function(i){
        if(i.title==name){
            id = i.id;
        }
    })
    return id;
}

Util.IsNullJson=(rowData)=>{
    if(JSON.stringify(rowData) == "{}"){
        return true;
    }else{
        return false;
    }
}


Util.isLogin=(type,old)=>{
    //实时判断是否登录状态
    var token = JSON.parse(Tool.localItem("User")) ? JSON.parse(Tool.localItem("User")).token : '';
    var flag=true;
    if(!(token && token.length>0)){//为空，去登录
        if(type==1){
            return false;
        }else{
            hashHistory.push("/login");
            return false;
        }
    }
    return flag;
}

Util.ComUserLogin=()=>{
    var nowUrl = window.location.href;
    console.log("链接"+nowUrl)
    if(!Util.isLogin(1,0)){
        if(!(nowUrl.indexOf("/login")>-1)){
            hashHistory.push("/login");
        }
    }
}

export default Util;